package com.example.elmmerchants.Setting;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Shop_Setting_Page extends AppCompatActivity {

    TextView shop_name_text_view, category, is_open_shop_text_view, shop_phone_number, shop_open_time, shop_place_text_view;
    ImageView shop_pic_image_view;
    RelativeLayout set_return_address, set_packing_fee, check_licence;

    String email, shop_name, first_category, second_category, contact_phone_number, open_hour, close_hour, open_hour_2, close_hour_2, shop_place;
    byte[] shop_pic = new byte[0];
    int is_open_shop;
    double packing_fee;
    boolean is_loading_data = false, is_loading_open_shop_page_data = false;
    AlertDialog dialog;
    String social_credit_code_in_licence, operator_name_in_licence, company_name_in_licence, company_place_in_licence, registered_capital_in_licence, issing_authority_in_licence, approval_date_in_licence, business_scope_in_licence;
    int is_long_term_vai_lidity_in_licence;
    String set_number_in_food_licence, set_business_name_in_food_licence, set_code_in_food_licence, set_representative_person_in_food_licence, set_live_where_in_food_licence_in_food_licence, set_business_place_in_food_licence_in_food_licence, set_sale_what_in_food_licence, set_business_scope_in_food_licence_text, set_validity_date_in_food_licence_text, set_license_number_in_food_licence_text, set_supervisory_agency_in_food_licence_text, set_supervisory_person_in_food_licence_text, set_report_phone_number_in_food_licence_text, set_issuer_in_food_licence_text;

    private class Open_Shop_Thread extends Thread {
        @Override
        public void run() {
            is_loading_open_shop_page_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        shop_pic = set.getBytes("shop_picture");
                    }
                    is_loading_open_shop_page_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_open_shop_page_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Bitmap decodedImage = BitmapFactory.decodeByteArray(shop_pic, 0, shop_pic.length);
                    shop_pic_image_view.setImageBitmap(decodedImage);
                }
            });
        }
    }

    private class Loading_Packing_Fee_Data_Thread extends Thread {
        @Override
        public void run() {
            is_loading_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        packing_fee = set.getDouble("packing_fee");
                    }
                    is_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    Intent intent = new Intent(Shop_Setting_Page.this, Set_Packing_Fee_Page.class);
                    intent.putExtra("email", email);
                    intent.putExtra("packing_fee", packing_fee);
                    startActivity(intent);
                }
            });
        }
    }

    private class Loading_Licence_Data_Thread extends Thread {
        @Override
        public void run() {
            is_loading_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant_licence_data WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        social_credit_code_in_licence = set.getString("social_credit_code");
                        operator_name_in_licence = set.getString("operator_name");
                        company_name_in_licence = set.getString("company_name");
                        company_place_in_licence = set.getString("company_place");
                        registered_capital_in_licence = set.getString("registered_capital");
                        issing_authority_in_licence = set.getString("issing_authority");
                        approval_date_in_licence = set.getString("approval_date");
                        business_scope_in_licence = set.getString("business_scope");
                        is_long_term_vai_lidity_in_licence = set.getInt("is_long_term_vai_lidity");
                    }

                    String sql2 = "SELECT * FROM merchant_food_business_licence WHERE email = ?;";
                    preparedStatement = connection.prepareStatement(sql2);
                    preparedStatement.setString(1, email);
                    ResultSet set2 = preparedStatement.executeQuery();
                    while (set2.next()) {
                        set_number_in_food_licence = set2.getString("number");
                        set_business_name_in_food_licence = set2.getString("business_name");
                        set_code_in_food_licence = set2.getString("code");
                        set_representative_person_in_food_licence = set2.getString("representative_person");
                        set_live_where_in_food_licence_in_food_licence = set2.getString("live_where");
                        set_business_place_in_food_licence_in_food_licence = set2.getString("business_place");
                        set_validity_date_in_food_licence_text = set2.getString("validity_date");
                        set_sale_what_in_food_licence = set2.getString("sale_what");
                        set_business_scope_in_food_licence_text = set2.getString("business_scope");
                        set_license_number_in_food_licence_text = set2.getString("license_number");
                        set_supervisory_agency_in_food_licence_text = set2.getString("supervisory_agency");
                        set_supervisory_person_in_food_licence_text = set2.getString("supervisory_person");
                        set_report_phone_number_in_food_licence_text = set2.getString("report_phone_number");
                        set_issuer_in_food_licence_text = set2.getString("issuer");
                    }
                    is_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    Intent intent = new Intent(Shop_Setting_Page.this, Set_Check_Licence_Page.class);
                    intent.putExtra("set_number_in_food_licence", set_number_in_food_licence);
                    intent.putExtra("set_business_name_in_food_licence", set_business_name_in_food_licence);
                    intent.putExtra("set_code_in_food_licence", set_code_in_food_licence);
                    intent.putExtra("set_representative_person_in_food_licence", set_representative_person_in_food_licence);
                    intent.putExtra("set_live_where_in_food_licence_in_food_licence", set_live_where_in_food_licence_in_food_licence);
                    intent.putExtra("set_business_place_in_food_licence_in_food_licence", set_business_place_in_food_licence_in_food_licence);
                    intent.putExtra("set_validity_date_in_food_licence_text", set_validity_date_in_food_licence_text);
                    intent.putExtra("set_sale_what_in_food_licence", set_sale_what_in_food_licence);
                    intent.putExtra("set_business_scope_in_food_licence_text", set_business_scope_in_food_licence_text);
                    intent.putExtra("set_license_number_in_food_licence_text", set_license_number_in_food_licence_text);
                    intent.putExtra("set_supervisory_agency_in_food_licence_text", set_supervisory_agency_in_food_licence_text);
                    intent.putExtra("set_supervisory_person_in_food_licence_text", set_supervisory_person_in_food_licence_text);
                    intent.putExtra("set_report_phone_number_in_food_licence_text", set_report_phone_number_in_food_licence_text);
                    intent.putExtra("set_issuer_in_food_licence_text", set_issuer_in_food_licence_text);
                    intent.putExtra("email", email);
                    intent.putExtra("social_credit_code_in_licence", social_credit_code_in_licence);
                    intent.putExtra("operator_name_in_licence", operator_name_in_licence);
                    intent.putExtra("company_name_in_licence", company_name_in_licence);
                    intent.putExtra("company_place_in_licence", company_place_in_licence);
                    intent.putExtra("registered_capital_in_licence", registered_capital_in_licence);
                    intent.putExtra("issing_authority_in_licence", issing_authority_in_licence);
                    intent.putExtra("approval_date_in_licence", approval_date_in_licence);
                    intent.putExtra("business_scope_in_licence", business_scope_in_licence);
                    intent.putExtra("is_long_term_vai_lidity_in_licence", is_long_term_vai_lidity_in_licence);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_setting_page);

        email = getIntent().getStringExtra("email");
        shop_name = getIntent().getStringExtra("shop_name");
        first_category = getIntent().getStringExtra("first_category");
        second_category = getIntent().getStringExtra("second_category");
        shop_pic = getIntent().getByteArrayExtra("shop_pic_string");
        is_open_shop = getIntent().getIntExtra("is_open_shop", 0);
        contact_phone_number = getIntent().getStringExtra("contact_phone_number");
        open_hour = getIntent().getStringExtra("open_hour");
        close_hour = getIntent().getStringExtra("close_hour");
        open_hour_2 = getIntent().getStringExtra("open_hour_2");
        close_hour_2 = getIntent().getStringExtra("close_hour_2");
        shop_place = getIntent().getStringExtra("shop_place");

        shop_name_text_view = findViewById(R.id.shop_name);
        category = findViewById(R.id.category);
        shop_pic_image_view = findViewById(R.id.shop_pic_image_view);
        is_open_shop_text_view = findViewById(R.id.is_open_shop_text_view);
        shop_phone_number = findViewById(R.id.shop_phone_number);
        shop_open_time = findViewById(R.id.shop_open_time);
        shop_place_text_view = findViewById(R.id.shop_place_text_view);
        set_return_address = findViewById(R.id.set_return_address);
        set_packing_fee = findViewById(R.id.set_packing_fee);
        check_licence = findViewById(R.id.check_licence);

        shop_name_text_view.setText(shop_name);
        shop_place_text_view.setText(shop_place);

        Open_Shop_Thread openShopThread = new Open_Shop_Thread();
        openShopThread.start();

        if (second_category.isEmpty()) {
            category.setText(first_category);
        }
        else {
            category.setText(first_category + ", " + second_category);
        }

        if (is_open_shop == 1) {
            is_open_shop_text_view.setText("营业中");
        }
        else {
            is_open_shop_text_view.setText("休息中");
        }

        shop_phone_number.setText(contact_phone_number);

        if (!open_hour_2.isEmpty()) {
            shop_open_time.setText(open_hour + "-" + close_hour + ", " + open_hour_2 + "-" + close_hour_2);
        }
        else {
            shop_open_time.setText(open_hour + "-" + close_hour);
        }

        set_return_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Shop_Setting_Page.this, Set_Return_Address_Page.class);
                intent.putExtra("shop_place", shop_place);
                startActivity(intent);
            }
        });

        set_packing_fee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();

                Loading_Packing_Fee_Data_Thread loadingPackingFeeDataThread = new Loading_Packing_Fee_Data_Thread();
                loadingPackingFeeDataThread.start();
            }
        });

        check_licence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();

                Loading_Licence_Data_Thread loadingLicenceDataThread = new Loading_Licence_Data_Thread();
                loadingLicenceDataThread.start();
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}